from openpyxl import load_workbook

from Common.Path_Constant import EXCEL_PATH, Basic_information
from Utils.YamlUtlis import YamlUtils
from Utils.logutil import logger


class ExcelUtil():

    def __init__(self):
        try:
            # 获取当前excel的workbook对象..
            self.read_book = load_workbook(EXCEL_PATH)
            # 获取当前的配置信息
            self.Basic_information = self.read_book[Basic_information]


        except FileNotFoundError as ec:
            if self.get_jibenxinxi() == None:
                logger.error('用例表不存在并且内部信息为空')
            else:
                pass

    # 用例
    def daoru_testcase(self):
        try:
            list_data = []
            for row in range(2, self.Basic_information.max_row + 1):
                sub_data = {}
                for column in range(1, self.Basic_information.max_column + 1):
                    sub_data[self.Basic_information.cell(1, column).value] = self.Basic_information.cell(row, column).value
                list_data.append(sub_data)
            YamlUtils().clear_yaml(file='testcase.yaml')
            YamlUtils().write_yaml(data=list_data, file='testcase.yaml')
        except AttributeError as ec:
            pass

    def get_testcase(self):
        return YamlUtils().read_yaml(file='testcase.yaml')


    # 获取最大行
    def get_max_row(self):
        return self.Basic_information.max_row